<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="60">
        <template slot-scope="{row}">
          <span>{{ row.Id }}</span>
        </template>
      </el-table-column>
      <el-table-column width="360px" align="center" label="商品名">
        <template slot-scope="{row}">
          <span>{{ cutString(row.Title + row.SubTitle, 55) }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="商家">
        <template slot-scope="{row}">
          <span>{{ row.MerchantName }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="分类">
        <template slot-scope="{row}">
          <span>{{ row.CatTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" align="center" label="单价">
        <template slot-scope="{row}">
          <span>{{ row.UnitPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" align="center" label="库存">
        <template slot-scope="{row}">
          <span>{{ row.Inventory }}</span>
        </template>
      </el-table-column>
      <el-table-column width="60px" align="center" label="热门">
        <template slot-scope="{row}">
          <i class="el-icon-check" style="color: green" v-if="row.IsHot"></i>
          <i class="el-icon-close" style="color: red" v-if="!row.IsHot"></i>
        </template>
      </el-table-column>
      <el-table-column width="60px" align="center" label="打折">
        <template slot-scope="{row}">
          <i class="el-icon-check" style="color: green" v-if="row.IsSale"></i>
          <i class="el-icon-close" style="color: red" v-if="!row.IsSale"></i>
        </template>
      </el-table-column>
      <el-table-column width="160px" label="添加日期" align="center">
        <template slot-scope="{row}">
          <span v-if="row.AddTime">{{ parseTime(row.AddTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" width="60">
        <template slot-scope="{row}">
          <i class="el-icon-check" style="color: green" v-if="row.Status"></i>
          <i class="el-icon-close" style="color: red" v-if="!row.Status"></i>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="{row}">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="handleAddWindow(row)"
          >
            编辑
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="handleStatus(row)"
          >
            <span v-if="row.Status">禁用</span>
            <span v-if="!row.Status">启用</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="searchData.page_index" :limit="searchData.page_size" :page-sizes="pageSizes" @pagination="getList" />

    <!-- 弹窗  -->
    <el-dialog title="新增商户" :visible.sync="dialogAddFormVisible">
      <el-form ref="addForm" :rules="rules" :model="addData" label-position="left" label-width="90px" style="width: 800px; margin-left:50px;">
        <el-form-item label="商户名称" prop="name">
          <el-input v-model="addData.name" style="width: 380px;" />
        </el-form-item>
        <el-form-item label="商户简介" prop="summary">
          <el-input v-model="addData.summary" style="width: 380px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          Cancel
        </el-button>
        <el-button type="primary" @click="handleAddSubmit()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { goodsList, goodsDetail, goodsStatus } from '@/api/commerce'

import waves from '@/directive/waves'
import { parseTime } from '@/utils/index'
import { cutStr } from '@/utils/common'
import Pagination from '@/components/Pagination'

export default {
  name: 'GoodsList',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      list: [],
      pageSizes: [10, 15, 20, 25, 30, 50, 100],
      listLoading: true,
      currentAction: '添加商户',
      total: 0,
      searchData: {
        page_index: 1,
        page_size: 15
      },
      currentUser: {},
      addData: {
        id: 0,
        name: '',
        summary: ''
      },
      dialogAddFormVisible: false,
      rules: {
        name: [
          { type: 'string', message: '商户名 6 - 24位', trigger: 'blur' },
          { required: true, message: '需要填写商户名', trigger: 'blur' }
        ],
        summary: [
          { required: true, message: '需要填写简介', trigger: 'blur' },
          { type: 'string', message: '简介最长128个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    resetAddData() {
      this.addData = {
        id: 0,
        name: '',
        summary: ''
      }
    },
    getList() {
      this.listLoading = true
      goodsList(this.searchData).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 300)
      })
    },
    handleFilter() {
      this.searchData.page_index = 1
      this.getList()
    },
    handleAddWindow(row) {
      this.resetAddData()
      if (row !== '') {
        this.addData.summary = row.Summary
        this.addData.name = row.Name
        this.addData.id = row.Id
      }
      this.dialogAddFormVisible = true
      this.$nextTick(() => {
        this.$refs['addForm'].clearValidate()
      })
    },
    handleStatus(row) {
      if (+row.Id === 0) {
        this.$message({
          type: 'error',
          message: '请选择用户'
        })
        return
      }

      this.$confirm('此操作将 禁用/启用 指定山品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        goodsStatus(row.Id).then((response) => {
          if (response.success) {
            this.$message({
              type: 'success',
              message: '设置成功!'
            })
            this.getList()
          } else {
            this.$message({
              type: 'error',
              message: response.msg
            })
          }
        }).catch(err => {
          this.$message({
            type: 'success',
            message: err
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    parseTime(time, format) {
      return parseTime(time, format)
    },
    cutString(str, number) {
      return cutStr(str, number)
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
